import {AppConfig} from '../config';
import {Injectable} from '@angular/core';
import {InterceptorService} from './interceptor/interceptor-service';

@Injectable()
export class AuthService {
  constructor(private http: InterceptorService) {
  }

  private readonly param = 'auth/login';
  /**
   * 重定向Url
   *
   * @private
   * @type {string}
   * @memberof AuthService
   */
  private _redirectUrl: string = '';

  /**
   * 验证结果
   *
   * @private
   * @type {boolean}
   * @memberof AuthService
   */
  private _authReason: boolean;
  /**
   * 登录进度
   *
   * @private
   * @type {boolean}
   * @memberof AuthService
   */
  private _loginProgress: boolean;

  private get url(): string {
    return `${AppConfig.APP_RESTFUL}/${this.param}`;
  }

  /**
   * 验证结果
   * true：已通过，false：未通过
   * @readonly
   * @type {boolean}
   * @memberOf AuthService
   */
  get authReason(): boolean {
    return this._authReason;
  }

  /**
   * 登录进度
   * true：请求结束，false：请求正在进行，未结束
   * @readonly
   * @type {boolean}
   * @memberOf AuthService
   */
  get loginProgress(): boolean {
    return this._loginProgress;
  }

  /**
   * 读取或设置重定向Url
   *
   * @readonly
   * @type {string}
   * @memberOf AuthService
   */
  get redirectUrl(): string {
    return this._redirectUrl;
  }

  /**
   * 设置重定向Url
   *
   *
   * @memberOf AuthService
   */
  set redirectUrl(url: string) {
    this._redirectUrl = url;
  }

  login(userName: string, passWord: string, callbackURL: string) {
    this._authReason = false;
    this._loginProgress = true;
    return this.http.post(this.url, {
      userName: userName,
      passWord: passWord,
      callbackURL: callbackURL
    }).toPromise().then(res => {
      this._authReason = true;
      return res.data;
    }).catch(res => {
      console.log(res);
    });
  }
}
